package com.demo.controller;

import com.demo.pojo.User;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author Windy
 * @Date 2022/9/9 20:02
 * @Version 1.0
 **/
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("findAll")
    public ModelAndView findAll(){
        List<User> all = userService.findAll();
        ModelAndView model = new ModelAndView();
        model.addObject("users",all);
        model.setViewName("findAll");
        return model;
    }

    @RequestMapping("addUser")
    public String addUser(User user) {
        System.out.println(user);
        userService.insertUser(user);
        System.out.println("------------");
        return "redirect:/user/findAll";
    }
}
